<?php

namespace App\Recharge\Api;

use App\Models\Admin\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Shuoruan extends AbstractApi
{
    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        // 会员充值
        $info1 = [
            'action'        => 'CZ',//指定编码
            'chargeAcct'    => $bean->getAccount(), //要充值的用户
            'chargeType'    => 4,//充值类型
            'chargeAmount'  => $bean->getRechargeNum() * $bean->getStartCharging(),
            'productSn'     => $bean->getGoodsId(), //商品编号id
            'retUrl'        => $bean->getNotifyUrl(), //异步回调地址
        ];
        $info = [];
        if ($bean->getProductType() == 0){
            $info = $info1;
        }
        $info['orderId'] = $bean->getOrderNo(); //订单号
        $info = json_encode($info);
        return [
            'agentAccount'  => $bean->getSupplierNum(),
            'sign'          => md5($info. $bean->getSecretKey()),
            'busiBody'      => json_decode($info,true),
        ];
    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $order_sn = $request->input('Orderid', '');
        $status = match ($request->input('Orderstatu_int')) {
            default     => 0,
            16          => 1,
            20,21,26    => 2,
        };
        return $this->apiNotifyData($order_sn, $status)->setExecute(true)->setRechargeSuccess($status == 1);
    }
}
